package com.aigou.controller;

import com.aigou.entity.Product;
import com.aigou.entity.User;
import com.aigou.service.ProductService;
import com.aigou.service.UserService;
import com.aigou.service.impl.ProductServiceImpl;
import com.aigou.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/saveProduct")
public class SaveProductController extends HttpServlet {

    //调用service
    private ProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, IOException {
        //获得用户id
        String id = req.getParameter("id");
        String product_name = req.getParameter("product_name");
        String product_image = req.getParameter("product_image");
        Double price = Double.valueOf(req.getParameter("price"));
        String product_type = req.getParameter("product_type");
        String product_desc = req.getParameter("product_desc");
        String create_time = req.getParameter("create_time");
        String product_brand = req.getParameter("product_brand");

        Product product = new Product(id,product_name,product_image,price,product_type,product_desc,create_time,product_brand);
        //查询用户
        boolean rs = productService.save(product);
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(String.valueOf(rs));
    }
}
